package net.ashrentum.utils;

import java.util.regex.Pattern;

public enum IntervalType {
	
	/** lower bound exclusive, upper bound exclusive (e.g., (3,4)) */
	EXC_EXC("(", ")"),
	/** lower bound inclusive, upper bound exclusive (e.g., [3,4)) */
	INC_EXC("[", ")"),
	/** lower bound exclusive, upper bound inclusive (e.g., (3,4]) */
	EXC_INC("(", "]"),
	/** lower bound inclusive, upper bound inclusive (e.g., [3,4]) */
	INC_INC("[", "]");
	
	
	String leftToken;
	String rightToken;
	Pattern pattern;
	
	static final String NUMBER_PATTERN = 			"[0-9]*(?:.[0-9]+)?"; //Allow .9 but no . neither 0.
	static final String GROUPED_NUMBER_PATTERN = 	"(" + NUMBER_PATTERN + ")";
	
	/** This position is defined by @see createIntervalPatternString */
	static final int LOWER_LIMIT = 1;
	/** This position is defined by @see createIntervalPatternString */
	static final int UPPER_LIMIT = 2;
	
	
	String cancelSpecial(String token) {
		return "\\" + token;
	}
	
	final String createIntervalPatternString(String leftToken, String rightToken) {
		this.leftToken  = leftToken;
		this.rightToken = rightToken;
		//TODO implement with StringBuilder
		return "\\s*" + cancelSpecial(leftToken) + "\\s*" + GROUPED_NUMBER_PATTERN + "\\s*" +
				"," +
				"\\s*" + GROUPED_NUMBER_PATTERN + "\\s*" + cancelSpecial(rightToken) + "\\s*"; 
	}
	
	IntervalType(String left, String right) {
		this.pattern = Pattern.compile(createIntervalPatternString(left, right));
	}
}
